package com.controller.developerPortal;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import model.User;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import dao.UserDAO;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/upload/")
public class UploadFileController {
	ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("springConfigSQL.xml");
	UserDAO userDAO = ctx.getBean("userDAO",UserDAO.class);
	
		   		    @RequestMapping(value = "/", method = RequestMethod.GET)
		    public String index(Model model) {
		   		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		   		if (!(authentication instanceof AnonymousAuthenticationToken)) {
					UserDetails userDetails = (UserDetails) authentication.getPrincipal();
					String userName = userDetails.getUsername();
					User user = userDAO.getByUserName(userName);
					model.addAttribute("name",  user.getFirstName()+ "  " + user.getLastName());
					model.addAttribute("idCardNumber", user.getIdCardNumber());
					model.addAttribute("address", user.getAddress());
					model.addAttribute("phone", user.getPhone());
					model.addAttribute("email", user.getEmail());
				}
		        return "uploadFile";
		   		    }
		   		    
		   		    
		   		 @RequestMapping(value = "/", method = RequestMethod.POST)
		   	    public String save(@RequestParam List<MultipartFile> files, Model model) {
		   	        model.addAttribute("files", files);	
//		   	        MultipartFile file = files.get(0);
		   	        for (MultipartFile file : files) {
			   	        String fileName = file.getOriginalFilename();
			   	        if (!file.isEmpty()){
							try {
								byte[] bytes = file.getBytes();
								File serverFile = new File("C:\\SaveYourFileHere\\" + fileName);
								BufferedOutputStream stream = new BufferedOutputStream(
										new FileOutputStream(serverFile));
								stream.write(bytes);
								stream.close();
								
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
			   		 }
					}
		   	        return "uploadFile";
		   		}
		   		 
//------------------------------------------------------------------------------------------------------------------------		   		 
		   		 
		   	    @RequestMapping(value = "/first_complex", method = RequestMethod.GET)
			    public String indexComplexOne(Model model) {
		   	    	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
			   		if (!(authentication instanceof AnonymousAuthenticationToken)) {
						UserDetails userDetails = (UserDetails) authentication.getPrincipal();
						String userName = userDetails.getUsername();
						User user = userDAO.getByUserName(userName);
						model.addAttribute("name",  user.getFirstName()+ "  " + user.getLastName());
						model.addAttribute("idCardNumber", user.getIdCardNumber());
						model.addAttribute("address", user.getAddress());
						model.addAttribute("phone", user.getPhone());
						model.addAttribute("email", user.getEmail());
					}
			        return "uploadFileTypeOne";
			   		    }
			   		    
			   		    
			   		 @RequestMapping(value = "/first_complex", method = RequestMethod.POST)
			   	    public String saveComplexOne(@RequestParam List<MultipartFile> files, Model model) {
			   	        model.addAttribute("files", files);	
//			   	        MultipartFile file = files.get(0);
			   	        for (MultipartFile file : files) {
				   	        String fileName = file.getOriginalFilename();
				   	        if (!file.isEmpty()){
								try {
									byte[] bytes = file.getBytes();
									File serverFile = new File("C:\\SaveYourFileHere\\" + fileName);
									BufferedOutputStream stream = new BufferedOutputStream(
											new FileOutputStream(serverFile));
									stream.write(bytes);
									stream.close();
									
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
				   		 }
						}
			   	        return "uploadFileTypeOne";
			   		}

//------------------------------------------------------------------------------------------------------------------------		   		 
			   		 
				   	    @RequestMapping(value = "/second_complex", method = RequestMethod.GET)
					    public String indexSecondOne(Model model) {
				   	    	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
					   		if (!(authentication instanceof AnonymousAuthenticationToken)) {
								UserDetails userDetails = (UserDetails) authentication.getPrincipal();
								String userName = userDetails.getUsername();
								User user = userDAO.getByUserName(userName);
								model.addAttribute("name",  user.getFirstName()+ "  " + user.getLastName());
								model.addAttribute("idCardNumber", user.getIdCardNumber());
								model.addAttribute("address", user.getAddress());
								model.addAttribute("phone", user.getPhone());
								model.addAttribute("email", user.getEmail());
							}
					        return "uploadFileTypeTwo";
					   		    }
				   	 
				   	    @RequestMapping(value = "/third_complex", method = RequestMethod.GET)
					    public String indexThirdOne(Model model) {
				   	    	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
					   		if (!(authentication instanceof AnonymousAuthenticationToken)) {
								UserDetails userDetails = (UserDetails) authentication.getPrincipal();
								String userName = userDetails.getUsername();
								User user = userDAO.getByUserName(userName);
								model.addAttribute("name",  user.getFirstName()+ "  " + user.getLastName());
								model.addAttribute("idCardNumber", user.getIdCardNumber());
								model.addAttribute("address", user.getAddress());
								model.addAttribute("phone", user.getPhone());
								model.addAttribute("email", user.getEmail());
							}
					        return "uploadFileTypeThree";
					   		    }

//------------------------------------------------------------------------------------------------------------------------		   		 
	 
   @RequestMapping(value = "/fourth_complex", method = RequestMethod.GET)
public String indexFourthOne(Model model) {
   	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (!(authentication instanceof AnonymousAuthenticationToken)) {
		UserDetails userDetails = (UserDetails) authentication.getPrincipal();
		String userName = userDetails.getUsername();
		User user = userDAO.getByUserName(userName);
		model.addAttribute("name",  user.getFirstName()+ "  " + user.getLastName());
		model.addAttribute("idCardNumber", user.getIdCardNumber());
		model.addAttribute("address", user.getAddress());
		model.addAttribute("phone", user.getPhone());
		model.addAttribute("email", user.getEmail());
	}
    return "uploadFileTypeFour";
		    }

   @RequestMapping(value = "/fifth_complex", method = RequestMethod.GET)
public String indexFifthOne(Model model) {
   	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (!(authentication instanceof AnonymousAuthenticationToken)) {
		UserDetails userDetails = (UserDetails) authentication.getPrincipal();
		String userName = userDetails.getUsername();
		User user = userDAO.getByUserName(userName);
		model.addAttribute("name",  user.getFirstName()+ "  " + user.getLastName());
		model.addAttribute("idCardNumber", user.getIdCardNumber());
		model.addAttribute("address", user.getAddress());
		model.addAttribute("phone", user.getPhone());
		model.addAttribute("email", user.getEmail());
	}
    return "uploadFileTypeFive";
		    }
   @RequestMapping(value = "/fifth_one_complex", method = RequestMethod.GET)
   public String indexFifthTwo(Model model) {
      	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
   		if (!(authentication instanceof AnonymousAuthenticationToken)) {
   		UserDetails userDetails = (UserDetails) authentication.getPrincipal();
   		String userName = userDetails.getUsername();
   		User user = userDAO.getByUserName(userName);
   		model.addAttribute("name",  user.getFirstName()+ "  " + user.getLastName());
   		model.addAttribute("idCardNumber", user.getIdCardNumber());
   		model.addAttribute("address", user.getAddress());
   		model.addAttribute("phone", user.getPhone());
   		model.addAttribute("email", user.getEmail());
   	}
       return "uploadFileTypeFiveOne";
   		    }
   @RequestMapping(value = "/fifth_two_complex", method = RequestMethod.GET)
   public String indexFifthThree(Model model) {
      	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
   		if (!(authentication instanceof AnonymousAuthenticationToken)) {
   		UserDetails userDetails = (UserDetails) authentication.getPrincipal();
   		String userName = userDetails.getUsername();
   		User user = userDAO.getByUserName(userName);
   		model.addAttribute("name",  user.getFirstName()+ "  " + user.getLastName());
   		model.addAttribute("idCardNumber", user.getIdCardNumber());
   		model.addAttribute("address", user.getAddress());
   		model.addAttribute("phone", user.getPhone());
   		model.addAttribute("email", user.getEmail());
   	}
       return "uploadFileTypeFiveTwo";
   		    }
   @RequestMapping(value = "/sixth_complex", method = RequestMethod.GET)
   public String indexSixthOne(Model model) {
      	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
   		if (!(authentication instanceof AnonymousAuthenticationToken)) {
   		UserDetails userDetails = (UserDetails) authentication.getPrincipal();
   		String userName = userDetails.getUsername();
   		User user = userDAO.getByUserName(userName);
   		model.addAttribute("name",  user.getFirstName()+ "  " + user.getLastName());
   		model.addAttribute("idCardNumber", user.getIdCardNumber());
   		model.addAttribute("address", user.getAddress());
   		model.addAttribute("phone", user.getPhone());
   		model.addAttribute("email", user.getEmail());
   	}
       return "uploadFileTypeSix";
   		    }
   @RequestMapping(value = "/sixth_one_complex", method = RequestMethod.GET)
   public String indexSixthTwo(Model model) {
      	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
   		if (!(authentication instanceof AnonymousAuthenticationToken)) {
   		UserDetails userDetails = (UserDetails) authentication.getPrincipal();
   		String userName = userDetails.getUsername();
   		User user = userDAO.getByUserName(userName);
   		model.addAttribute("name",  user.getFirstName()+ "  " + user.getLastName());
   		model.addAttribute("idCardNumber", user.getIdCardNumber());
   		model.addAttribute("address", user.getAddress());
   		model.addAttribute("phone", user.getPhone());
   		model.addAttribute("email", user.getEmail());
   	}
       return "uploadFileTypeSixOne";
   		    }
   @RequestMapping(value = "/sixth_two_complex", method = RequestMethod.GET)
   public String indexSixthThree(Model model) {
      	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
   		if (!(authentication instanceof AnonymousAuthenticationToken)) {
   		UserDetails userDetails = (UserDetails) authentication.getPrincipal();
   		String userName = userDetails.getUsername();
   		User user = userDAO.getByUserName(userName);
   		model.addAttribute("name",  user.getFirstName()+ "  " + user.getLastName());
   		model.addAttribute("idCardNumber", user.getIdCardNumber());
   		model.addAttribute("address", user.getAddress());
   		model.addAttribute("phone", user.getPhone());
   		model.addAttribute("email", user.getEmail());
   	}
       return "uploadFileTypeSixTwo";
   		    }
   @RequestMapping(value = "/seventh_complex", method = RequestMethod.GET)
   public String indexSeventhOne(Model model) {
      	Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
   		if (!(authentication instanceof AnonymousAuthenticationToken)) {
   		UserDetails userDetails = (UserDetails) authentication.getPrincipal();
   		String userName = userDetails.getUsername();
   		User user = userDAO.getByUserName(userName);
   		model.addAttribute("name",  user.getFirstName()+ "  " + user.getLastName());
   		model.addAttribute("idCardNumber", user.getIdCardNumber());
   		model.addAttribute("address", user.getAddress());
   		model.addAttribute("phone", user.getPhone());
   		model.addAttribute("email", user.getEmail());
   	}
       return "uploadFileTypeSeven";
   		    }

}